/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package client.views;

import client.beans.AuthBean;
import client.beans.ServerBean;
import client.types.Account;
import client.types.TransactionType;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.xml.bind.JAXBException;
import types.ResponseXML;

/**
 *
 * @author efim
 */
@ManagedBean
@SessionScoped
public class AddTransactionView 
{
    @EJB
    private AuthBean authBean;
    @EJB
    private ServerBean serverBean;
    
    
       
    private String name;

    /**
     * Get the value of name
     *
     * @return the value of name
     */
    public String getName() {
        return name;
    }

    /**
     * Set the value of name
     *
     * @param name new value of name
     */
    public void setName(String name) {
        this.name = name;
    }

    private double amount;

    /**
     * Get the value of amount
     *
     * @return the value of amount
     */
    public double getAmount() {
        return amount;
    }

    /**
     * Set the value of amount
     *
     * @param amount new value of amount
     */
    public void setAmount(double amount) {
        this.amount = amount;
    }

    private Long account;

    /**
     * Get the value of account
     *
     * @return the value of account
     */
    public Long getAccount() {
        return account;
    }

    /**
     * Set the value of account
     *
     * @param account new value of account
     */
    public void setAccount(Long account) {
        this.account = account;
    }

    private Long type;

    /**
     * Get the value of type
     *
     * @return the value of type
     */
    public Long getType() {
        return type;
    }

    /**
     * Set the value of type
     *
     * @param type new value of type
     */
    public void setType(Long type) {
        this.type = type;
    }

    /**
     * Creates a new instance of AddTransactionView
     */
    public AddTransactionView() {
        
    }
    @PostConstruct
    public void Init()
    {      
        try {            
            Map<String,String> params = new HashMap<>();
            params.put("user_id", String.valueOf(authBean.getUserId()));
            Object answer = serverBean.queryToServer("GetAccounts", params);
            ResponseXML<Account> acc = (ResponseXML<Account>)answer;
            accounts = acc.getData().getItems();
            
            params = new HashMap<>();                       
            answer = serverBean.queryToServer("GetTransactionTypes", params);
            ResponseXML<TransactionType> tt = (ResponseXML<TransactionType>)answer;
            transactionTypes = tt.getData().getItems();
            
        } catch (JAXBException ex) {
            Logger.getLogger(AddTransactionView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AddTransactionView.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }
    
    private List<Account> accounts;
    
    public List<Account> getAccounts()
    {      
        return accounts;
    }
    
    public void postTransaction()
    {       
        try {
            
            Map<String,String> params = new HashMap<>();
            params.put("name", this.getName());
            params.put("amount", String.valueOf(amount));
            params.put("account_id", this.account.toString());
            params.put("type_id", this.type.toString());
            serverBean.queryToServer("CreateTransaction", params);
            
        } catch (JAXBException ex) {
            Logger.getLogger(AddTransactionView.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AddTransactionView.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
    }
    
    private List<TransactionType> transactionTypes;
    
    public List<TransactionType> getTransactionType()
    {        
        return transactionTypes;
    }
}
